from django.shortcuts import render

from finance.models import finance
from staff.models import department, project


def query(request):
    department_record = department.objects.all()  # 查询所有部门信息
    finance_record = []

    for departments in department_record:  # 遍历部门
        project_record = project.objects.filter(department_id=departments.id)  # 查询每个部门内部所有项目
        if project_record.exists():
            for projects in project_record:  # 遍历每个项目
                finances = finance.objects.filter(project_id=projects.id).order_by('month')
                sum = 0
                for month_finance in finances:
                    sum += month_finance.money
                finance_record.append({'department': departments.name, 'project': projects.name, 'finance': finances, 'sum': sum})
    for record in finance_record:
        print(record)
    return render(request, 'finance/financeInfo.html', {'finance_record': finance_record})
